package com.prp.gwt.client.admin;

import java.util.HashSet;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.prp.gwt.client.ClientUtility;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.IListener;
import com.prp.gwt.client.vo.CommunityList;
import com.prp.gwt.client.vo.User;
import com.prp.gwt.client.vo.UserList;
import com.prp.gwt.client.widgets.TopPanel;
import com.prp.gwt.client.widgets.admin.AdminListPanel;
import com.prp.gwt.client.widgets.admin.AdminView;
import com.prp.gwt.client.widgets.admin.CommunityListPanel;
import com.prp.gwt.client.widgets.admin.CommunityMember;
import com.prp.gwt.client.widgets.admin.InactivatedUserList;
import com.prp.gwt.client.widgets.admin.UserQueryPanel;

public class AdminListener implements IListener {

	private static AdminListener instance;
	private static AdminServiceAsync service = GWT.create(AdminService.class);
	HashSet<String> emailSuggest = new HashSet<String>();
	HashSet<String> nameSuggest = new HashSet<String>();

	public static AdminListener getInstance() {
		if (instance == null)
			instance = new AdminListener();
		return instance;
	}

	public void verify() {
		service.verify(new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result)
					Admin.init();
				else
					Window.Location.replace(Host.getENTRY());
			}
		});
	}

	public void updateUser() {
		service.getUser(new AsyncCallback<User>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(User result) {
				TopPanel.getInstance().load(result);
			}
		});
	}

	public void updateAdminList() {
		service.getAdminList(new AsyncCallback<UserList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserList result) {
				AdminListPanel.getAdminList().load(result);
			}
		});
	}
	
	public void updateRootList() {
		service.getRootList(new AsyncCallback<UserList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserList result) {
				AdminListPanel.getRootList().load(result);
			}
		});
	}

	public void divideCommunity() {
		service.divideCommunity(new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Void result) {
				CommunityListPanel.getInstance().setMessage("服务器正在社区划分请求");
			}
		});
	}

	public void updateCommunityList() {
		service.isDivideRunning(new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					service
							.getCommunityList(new AsyncCallback<CommunityList>() {

								@Override
								public void onFailure(Throwable caught) {
									ClientUtility.handleException(caught);
								}

								@Override
								public void onSuccess(CommunityList result) {
									CommunityListPanel.getInstance().load(
											result);
								}

							});
				} else {
					CommunityListPanel.getInstance().setMessage("社区划分算法正在运行");
				}
			}
		});
	}

	public void removeAdmin(String email) {
		service.removeAdmin(email, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					updateAdminList();
					AdminListPanel.getAdminList().showMessage("移除管理员成功,正在刷新");
				} else {
					AdminListPanel.getAdminList().showMessage("移除失败");
				}
			}
		});
	}

	public void removeRoot(String email) {
		service.removeAdmin(email, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					updateRootList();
					AdminListPanel.getRootList().showMessage("移除超级管理员成功");
				} else
					AdminListPanel.getRootList().showMessage("移除失败");
			}
		});
	}

	public void addRoot(String email) {
		service.addRoot(email, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					updateRootList();
					AdminListPanel.getRootList().showMessage("添加超级管理员成功,正在刷新");
				} else
					AdminListPanel.getRootList().showMessage("添加失败");
			}
		});
	}

	public void addAdmin(String email) {
		service.addAdmin(email, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					AdminListPanel.getAdminList().showMessage("添加管理员成功,正在刷新");
					updateAdminList();
				} else
					AdminListPanel.getAdminList().showMessage("添加失败");
			}
		});
	}

	public void getEmailSuggest(String query) {
		if (emailSuggest.contains(query))
			service.getEmailSuggest(query, new AsyncCallback<List<String>>() {
				@Override
				public void onFailure(Throwable caught) {
					ClientUtility.handleException(caught);
				}

				@Override
				public void onSuccess(List<String> result) {
					AdminView.getInstance().setEmailOracle(result);
				}
			});
		else
			emailSuggest.add(query);
	}

	public void getNameSuggest(String query) {
		if (nameSuggest.contains(query))
			service.getNameSuggest(query, new AsyncCallback<List<String>>() {
				@Override
				public void onFailure(Throwable caught) {
					ClientUtility.handleException(caught);
				}

				@Override
				public void onSuccess(List<String> result) {
					AdminView.getInstance().setNameOracle(result);
				}
			});
		else
			nameSuggest.add(query);
	}

	public void getCommunityMemeber(final int communityId) {
		service.getCommunityMemeber(communityId, new AsyncCallback<UserList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserList result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				else
					CommunityMember.getInstance().update(result, communityId);
			}
		});
	}

	public void getInactivatedUserList() {
		service.getInactivatedUserList(new AsyncCallback<UserList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserList result) {
				InactivatedUserList.getInstance().update(result);
			}
		});
	}

	public void activateUser(String email) {
		service.activateUser(email, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					getInactivatedUserList();
					InactivatedUserList.getInstance().showMessage("激活成功,正在刷新");
				} else
					InactivatedUserList.getInstance().showMessage(
							"激活失败,列表已过期,请刷新");
			}
		});
	}

	public void deleteUser(String email) {
		service.deleteUser(email, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					getInactivatedUserList();
					InactivatedUserList.getInstance().showMessage("删除成功,正在刷新");
				} else
					InactivatedUserList.getInstance().showMessage(
							"删除失败,列表已过期,请刷新");
			}
		});
	}

	public void searchForEmail(String email) {
		service.searchByEmail(email, new AsyncCallback<User>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(User result) {
				UserList userList = new UserList();
				if (result != null)
					userList.addUser(result);
				UserQueryPanel.getInstance().update(userList);
			}
		});
	}

	public void searchForName(String name) {
		service.searchByName(name, new AsyncCallback<UserList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserList result) {
				UserQueryPanel.getInstance().update(result);
			}
		});
	}

	@Override
	public void signOut() {
		service.signOut(new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Void result) {
				Window.Location.replace(Host.getENTRY());
			}
		});
	}

}
